package by.grsu.pereverzev.page;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.CompoundPropertyModel;

import by.grsu.pereverzev.app.PereverzevWebApplication;
import by.grsu.pereverzev.dao.impl.PassengerDao;
import by.grsu.pereverzev.entity.Passenger;


public class PassengerEditPage extends WebPage {
	public PassengerEditPage(final Passenger passenger) {
		super();
		

		final Form<Passenger> form = new Form<Passenger>("form", new CompoundPropertyModel<Passenger>(passenger));
		add(form);
		

		// add input fields to form
		form.add(new TextField<String>("name"));
		
		form.add(new SubmitLink("save") {
			@Override
			public void onSubmit() {
				super.onSubmit();

				PassengerDao passengerDao = new PassengerDao(PereverzevWebApplication.DATA_XML_FOLDER);
				if (passenger.getId() == null) {
					System.out.println("save="+passenger);
					passengerDao.saveNew(passenger);
				} else {
					System.out.println("update="+passenger);
					passengerDao.update(passenger);
				}

				setResponsePage(new PassengersListPage());
			}
		});
		
		form.add(new Link("cancel") {
			@Override
			public void onClick() {
				setResponsePage(new PassengersListPage());
			}
		});
		
	}
}
